#ifndef COLOR_H
#define COLOR_H

#include <string>
#include <sstream>

struct Color
{
  double r, g, b;
  Color() { r = g = b = 0; }
  Color operator*(const Color &rhs) const
  {
    Color c = *this;
    c *= rhs;

    return c;
  }
  Color operator*(const double &x) const
  {
    Color c = *this;
    c *= x;

    return c;
  }
  Color operator/(const double &x) const
  {
    return (*this) * (1.0 / x);
  }
  Color operator+(const Color &b) const
  {
    Color c = *this;
    c.r += b.r;
    c.g += b.g;
    c.b += b.b;

    return c;
  }
  Color& operator+=(const Color &rhs)
  {
    r += rhs.r;
    g += rhs.g;
    b += rhs.b;

    return *this;
  }
  Color& operator*=(const double &x)
  {
    r *= x;
    g *= x;
    b *= x;

    return *this;
  }
  Color& operator*=(const Color &rhs)
  {
    r *= rhs.r;
    g *= rhs.g;
    b *= rhs.b;

    return *this;
  }
  std::string toHtml()
  {
    return "#" + toHex(r) + toHex(g) + toHex(b);
  }
  std::string toPpm()
  {
    return toInt(r) + " " + toInt(g) + " " + toInt(b);
  }
  private:
    std::string toInt(double val)
    {
      if (val < 0)
      {
        val = 0;
      }
      if (val > 1)
      {
        val = 1;
      }
      std::stringstream intStream;
      intStream << (int)(val * 255);
      return intStream.str();
    }
    std::string toHex(double val)
    {
      if (val < 0)
      {
        val = 0;
      }
      if (val > 1)
      {
        val = 1;
      }
      std::stringstream hexStream;
      std::string hexString;

      hexStream << std::hex << (int)(val * 255);
      hexStream >> hexString;

      if (hexString.size()==1)
      {
        hexString = "0" + hexString;
      }
      return hexString;
    }
};


#endif

